﻿using AdminBase.Common.Attributes;
using AdminBase.Common.Helpers;
using AdminBase.Interface;
using AdminBase.Model;
using AdminBase.Model.Common;
using AdminBase.Model.Entitys;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace AdminBase.webapi.Controllers
{
    /// <summary>
    /// 部门控制器
    /// </summary>
    [ApiController]
    [Authorize]
    [Route("[controller]")]
    [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
    [TypeFilter(typeof(AsyncExceptionFilterAttribute))]
    public class DepartmentsController : ControllerBase
    {
        // 业务接口
        private readonly IDepartmentsService _department;

        /// <summary>
        /// 构造注入
        /// </summary>
        /// <param name="department"></param>
        public DepartmentsController(IDepartmentsService department)
        {
            _department = department;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult Add(Departments entity)
        {
            //获取当前登录人信息 
            entity.CreateUserName = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "FullName")?.Value;
            entity.UpdateUserName = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "FullName")?.Value;
            return ResultHelper.Success(_department.Add(entity));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPut]
        public ApiResult Edit(Departments entity)
        {
            return ResultHelper.Success(_department.Edit(entity));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("{id}")]
        public ApiResult Del(long id)
        {
            return ResultHelper.Success(_department.Del(id));
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public ApiResult BatchDel([FromBody] List<long> ids)
        {
            return ResultHelper.Success(_department.BatchDel(ids));
        }

        /// <summary>
        /// 根据ID获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{id:long}")]
        public ApiResult GetDepartment(long id)
        {
            return ResultHelper.Success(_department.GetById(id));
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="queryParam">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult GetDepartments([FromQuery] DepartmentsQueryParam queryParam)
        {
            return ResultHelper.Success(_department.GetDepartments(queryParam));
        }

        /// <summary>
        /// 获取树形数据
        /// </summary>
        /// <returns></returns>        
        [Route("tree")]
        [HttpGet]
        public ApiResult GetTreeData()
        {
            return ResultHelper.Success(_department.GetTreeData());
        }
    }
}
